//
//  mapVIew.m
//  theCoupleLive
//
//  Created by USER on 2016/12/6.
//  Copyright © 2016年 USER. All rights reserved.
//

#import "mapVIew.h"

@interface mapVIew ()<MKMapViewDelegate,CLLocationManagerDelegate>
@property (nonatomic, strong) CLGeocoder *geoC;
@property(nonatomic,strong)MKMapView *mapview;

@end

@implementation mapVIew

-(instancetype)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if (self) {
        self.mapview = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        self.mapview.delegate=self;
        //显示用户当前的位置
        self.mapview.showsUserLocation = YES;
        //使用第三方的地图可以查找周边环境的餐馆，学校之类的
        /*
         MKMapTypeStandard 标准地图
         MKMapTypeSatellite 卫星地图
         MKMapTypeHybrid 混合地图
         */
        self.mapview.mapType = MKMapTypeStandard;
        
        //初始化的坐标
        CLLocationCoordinate2D coor2d = {33.00,112.52};
        //CLLocationCoordinate2D coor2d = {37.7,112.4};
        //显示范围，数值越大，范围就越大
        MKCoordinateSpan span = {5,5};
        MKCoordinateRegion region = {coor2d,span};
        //是否允许缩放，一般都会让缩放的
        //mapview.zoomEnabled = NO;
        //mapview.scrollEnabled = NO;
        //地图初始化时显示的区域
        [self.mapview setRegion:region];
//        [self addSubview:self.mapview];
        self.locationmanager = [[CLLocationManager alloc]init];
        
        //设置精度
        /*
         kCLLocationAccuracyBest
         kCLLocationAccuracyNearestTenMeters
         kCLLocationAccuracyHundredMeters
         kCLLocationAccuracyHundredMeters
         kCLLocationAccuracyKilometer
         kCLLocationAccuracyThreeKilometers
         */
        //设置定位的精度
        [self.locationmanager setDesiredAccuracy:kCLLocationAccuracyBest];
        //实现协议
        self.locationmanager.delegate = self;
//        NSLog(@"开始定位");
//        //开始定位
//        [self.locationmanager startUpdatingLocation];
//        self.mapview=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 250)];
        [self addSubview:self.mapview];
        
        
    }
    return self;
}

- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView{
    NSLog(@"开始定位");
    //开始定位
    [self.locationmanager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    //定位成功后在地图上移动到当前位置
    [self.locationmanager stopUpdatingLocation];
    NSLog(@"结束定位");
    NSString *strLat = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.latitude];
    NSString *strLng = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.longitude];
    NSLog(@"Lat: %@  Lng: %@", strLat, strLng);
    
    CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(newLocation.coordinate.latitude,newLocation.coordinate.longitude);
    float zoomLevel = 0.02;
    MKCoordinateRegion region = MKCoordinateRegionMake(coords,MKCoordinateSpanMake(zoomLevel, zoomLevel));
    [self.mapview setRegion:[self.mapview regionThatFits:region] animated:YES];
    
}




@end
